home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.4 KB

  1. Path: news.eclipse.net!usenet
  2. From: steve@eclipse.net (Steve Teale)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ help--probably a simple answer (short listing attached)
  5. Date: Mon, 05 Feb 1996 16:56:55 GMT
  6. Message-ID: <4f5cup$26u@lunar.eclipse.net>
  7. References: <4ehdkl$mj0@ixnews7.ix.netcom.com>
  8. NNTP-Posting-Host: ne_36.eclipse.net
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. mearrin@ix.netcom.com(Michael Arrington ) wrote:
  12.  
  13. >Hi,
  14.  
  15. >I have a question about C++.  I'm just learning C++ but I've been
  16. >writing programs in other languages for a number of years.  In this
  17. >problem, I'm creating a class to represent a 2D cellular automaton
  18. >(like Conway's Life). Here's the (partial) declaration and
  19. >implementation of the class:
  20.  
  21. >*******************
  22. >****FROM CELL.H****
  23. >*******************
  24.  
  25. >class CCellAuto
  26. >{
  27. >public:
  28. >    // Public constructors & destructors
  29. >    CCellAuto();                                // Default constructor
  30. >    CCellAuto(int xSize, int ySize);            // Overloaded
  31. >Constructor
  32. >    ~CCellAuto();                               // Default Destructor
  33. >    // Public accessor methods
  34. >    void BumpGen();
  35. >    void BumpPop();
  36. >    int GetGen() const;
  37. >    int GetPop() const;
  38. >    void RandomizeCells();
  39. >    void SetRules(int deathLow, int deathHigh, int birthLow, int
  40. >birthHigh);
  41. >    void UpdateCells();                                                
  42. >    void ResizeArrays(int xSize, int ySize);
  43.  
  44. >private: 
  45. >    // Private data members
  46. >    int xSize;              // X dimension of arrays
  47. >    int ySize;              // Y dimension of arrays
  48. >    int cell[1][1];         // Array holding live cells
  49. >    int old[1][1];          // Temp array
  50. >    int deathLow;           // Birth/Death rules
  51. >    int deathHigh;
  52. >    int birthLow;
  53. >    int birthHigh;
  54. >    int currGen;            // # of current generation
  55. >    int currPop;            // # of cells alive
  56. >    int wrap;               // Wrap to other side of array?
  57. >};
  58.  
  59. >*********************
  60. >****FROM CELL.CPP****
  61. >*********************
  62.  
  63. >// Default constructor
  64. >CCellAuto::CCellAuto()
  65. >{
  66. >    wrap = 1;                           // Set wrap to on
  67. >    currPop = 0;                        // Initialize currPopulation
  68. >    currGen = 1;                        // Initialize currGeneration
  69. >    ResizeArrays(10, 10);               // Set default array size
  70. >    SetRules(2, 4, 2, 4);               // Set default rules
  71. >    RandomizeCells();                   // Randomize cells
  72. >}
  73.  
  74. >// Overloaded Constructor
  75. >CCellAuto::CCellAuto(int x, int y)
  76. >{
  77. >    wrap = 1;                           // Set wrap to on
  78. >    currPop = 0;                        // Initialize currPopulation
  79. >    currGen = 1;                        // Initialize currGeneration
  80. >    ResizeArrays(x, y);                 // Size arrays to X by Y
  81. >    SetRules(2, 4, 2, 4);               // Set default rules
  82. >    RandomizeCells();                   // Randomize cells
  83. >}
  84.  
  85. >// Default Destructor
  86. >CCellAuto::~CCellAuto()
  87. >{
  88. >}
  89.  
  90. >// BumpGen()
  91. >void CCellAuto::BumpGen()
  92. >{
  93. >    currGen++;
  94. >}
  95.  
  96. >// BumpPop()
  97. >void CCellAuto::BumpPop()
  98. >{
  99. >    currPop++;
  100. >}         
  101.  
  102. >// GetGen()
  103. >int CCellAuto::GetGen() const
  104. >{
  105. >    return currGen;
  106. >}
  107.  
  108. >// GetPop()
  109. >int CCellAuto::GetPop() const
  110. >{
  111. >    return currPop;
  112. >}    
  113.  
  114. >// RandomizeCells()
  115. >void CCellAuto::RandomizeCells()
  116. >{
  117. >     // Omitted...Randomly sets cell[][] elements to either 1 or 0
  118. >}               
  119.  
  120. >// ResizeArrays()
  121. >void CCellAuto::ResizeArrays(int x, int y)
  122. >{
  123. >     // Omitted...Sets cell[][] and old[][] to dimensions x and y
  124. >}
  125.  
  126. >// SetRules()
  127. >void CCellAuto::SetRules(int a, int b, int c, int d)
  128. >{
  129. >    deathLow = a;
  130. >    deathHigh = b;
  131. >    birthLow = c;
  132. >    birthHigh = d;
  133. >}
  134.  
  135. >// UpdateCells()
  136. >void CCellAuto::UpdateCells()
  137. >{
  138. >     // Omitted...copies cell[][] to old[][] and creates next iteration
  139. >     // in new[][].  Calls BumpGen() when done.
  140. >}
  141.  
  142. >Whew!  I omitted a lot of that...but I wanted to give enough to let you
  143. >see any problems I might have created in it.  Now, when I declare a
  144. >CCellAuto object [[like this -- CCellAuto *Life1 = new CCellAuto(20,
  145. >20); ]]and try to access a member function [[like this -- cout <<
  146. >Life1->GetPop() << endl; ]] I get uninitialized variables.  That's
  147. >weird because I initialize them in the constructor.  Now, if I were to
  148. >call Life1->BumpPop() first...the GetPop() function returns the correct
  149. >number.  Why?  Here's my main() listing:
  150.  
  151. >*********************
  152. >****FROM LIFE.CPP****
  153. >*********************
  154.  
  155. >01   void main()
  156. >02   {
  157. >03       CCellAuto *Life1 = new CCellAuto(20,20);
  158. >04       if (Life1 == 0)
  159. >05           cout << "cannot allocate memory\n";
  160. >06       //Life1->BumpPop();
  161. >07       cout << "Life1 Pop: " << Life1->GetPop() << endl;
  162. >08       cout << "Life1 Gen: " << Life1->GetGen() << endl;
  163. >09       delete Life1;
  164. >10   }
  165.  
  166.  
  167. >As listed, lines 07 and 08 return 0 values for currPop and currGen.  If
  168. >you remove the comment marks from line 06, line 07 will return the
  169. >actual (albeit incremented) value of 1. I have no idea what is going on
  170. >here.  I don't think it's my logic--I've written this same program
  171. >before in BASIC and some other languages.  Any suggestions as to what's
  172. >going on?  Please help!
  173.  
  174. >If you have any answers, please e-mail to MEArrin@ix.netcom.com unless
  175. >you feel it would be beneficial to the forum.  Thanks in advance for
  176. >your help.
  177.  
  178. >Mike
  179.  
  180. >BTW:  I'm using MSC++ v7.0 if that's a help.
  181.  
  182. Your problem is probably in ResizeArrays, which you omitted.  You
  183. can't do it using the class definition you propose.
  184.  
  185.